package lzk.myshop.dao;

import lzk.myshop.Util.OrderToMer;
import lzk.myshop.pojo.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface OrderMapper{
    @Select("select * from `order` where `userid` = #{userid}")
    List<Order> getOrderByUserId(int userid);

    @Insert("insert into `order` (`userid`,`totalprice`,`starttime`,`overtime`,`status`,`receiver`,`phone`,`arrivaladdress`,`remark`) values(#{userid}, #{totalprice}, #{starttime}, #{overtime}, #{status},#{receiver},#{phone},#{arrivaladdress},#{remark})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int newOrder(Order order);



    @Select("SELECT od.id AS detailId, o.id AS orderid, o.starttime, o.overtime, o.receiver, o.phone, " +
            "o.arrivaladdress, od.goodsid AS goodsname, od.number, od.totalprice, o.remark " +
            "FROM `order` o " +
            "INNER JOIN orderdetail od ON o.id = od.orderid " +
            "WHERE od.merchantid = #{merchantid}")
    @Results({
            @Result(property = "detailId", column = "detailId"),
            @Result(property = "orderid", column = "orderid"),
            @Result(property = "starttime", column = "starttime"),
            @Result(property = "overtime", column = "overtime"),
            @Result(property = "receiver", column = "receiver"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "arrivaladdress", column = "arrivaladdress"),
            @Result(property = "goodsname", column = "goodsname"),
            @Result(property = "number", column = "number"),
            @Result(property = "totalprice", column = "totalprice"),
            @Result(property = "remark", column = "remark")
    })
    List<OrderToMer> getAllMerOrder(int merchantId);

    @Update("update `order` set `status`=2 where `id`=#{id} and `status`=1")
    int sentGoods(int id);

    @Update("update `order` set `status`=3 where `id`=#{id} and `status`=2")
    int getGoods(int id);

    @Select("select `totalprice` from `order` where `id` = #{id}")
    double getTotalPriceById(int ID);

    @Update("update `order` set `status`=1 where `id`=#{id} and `status`= 0")
    int payOrder(int id);
    @Update("UPDATE `order` SET `status` = -1 WHERE `id` = #{id} AND `status` = 0")
    int deleteOrder(int id);

    @Select("select `userid` from `order` where `id` = #{id}")
    int getUseridByOrderID(int id);


}
